<?php 
	session_start();
	$result = '';
	
	switch($_POST['ajax']) {
		
		case 'logout': 	unset($_SESSION); session_destroy(); $result = 1;	break;
			
		case 'create-db': 	$conn = mysql_connect($_POST['host'], $_POST['username'], $_POST['password']);
							if( ! $conn ) { $result = "Could not connect to MySQL Connection" . mysql_error(); }
							else {
								$retval = mysql_query( "CREATE Database `dbmseuf`", $conn );
								$result = (! $retval ) ? "Could not create database: " . mysql_error() : "Database test_db created successfully";
							}
							mysql_close($conn);
			break;
		
		case 'create-tables':
							$conn = mysql_connect($_POST['host'], $_POST['username'], $_POST['password']);
							if( ! $conn ) { $result = "Could not connect to MySQL Connection" . mysql_error(); }
							else {
								$sqlFileToExecute = 'db_mseuf.sql';
								mysql_select_db('dbmseuf', $conn);
								$sqlErrorCode = 0;
								// read the sql file
								$f 			= fopen($sqlFileToExecute,"r+");
								$sqlFile 	= fread($f, filesize($sqlFileToExecute));
								$sqlArray 	= explode(';',$sqlFile);
								foreach ($sqlArray as $stmt) {
									if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') {
										$result = mysql_query($stmt . ';');
										if (!$result) {
											$sqlErrorCode = mysql_errno();
											break;
										}
									}
									/*echo "<pre>";
										print_r($stmt);
									echo "</pre>";*/
								}
								$result = ($sqlErrorCode == 0) ? "Script is executed succesfully!" : "An error occured during installation!";
							}
							mysql_close($conn);
			break;
		
		default: break;
		
	}
	
	echo json_encode($result); exit();

?>